package dapps.web.monitor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.DateUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.monitor.Cominput;
import dapps.entity.monitor.Element;
import dapps.entity.monitor.Empk;
import dapps.entity.monitor.Indicator;
import dapps.entity.monitor.Servicedata;
import dapps.entity.monitor.Servicemain;
import dapps.entity.monitor.Servicepredi;
import dapps.entity.user.District;
import dapps.entity.user.User;
import dapps.service.ServiceException;
import dapps.service.monitor.MonitorManager;
import dapps.service.user.UserManager;

@SuppressWarnings("serial")
@Results({ @Result(name = CRUDActionSupport.RELOAD, location = "service.action?page.pageRequest=${page.pageRequest}&yearmonth=${yearmonth}", type = "redirect") })
public class ServiceAction extends CRUDActionSupport<Servicemain> {

	@Autowired
	private MonitorManager mManager;
	@Autowired
	private UserManager uManager;

	// 基本属性
	private Servicemain entity;
	private Servicedata servicedata;
	private Servicepredi servicepredi;
	private Long id;
	// private Page<User> page = new Page<User>(15);//每页5条记录
	private Page<Cominput> page = new Page<Cominput>(15);
	private Integer usercode;
	private String yearmonth;
	private Long uid;
	private Long districtid;
	private int inputcount;
	private List<District> districtlist;

	// 角色相关属性

	// 基本属性访问函数 //

	public Servicemain getModel() {
		return entity;
	}

	public Servicedata getServicedata() {
		return servicedata;
	}

	public Servicepredi getServicepredi() {
		return servicepredi;
	}

	public void setServicedata(Servicedata servicedata) {
		this.servicedata = servicedata;
	}

	public void setServicepredi(Servicepredi servicepredi) {
		this.servicepredi = servicepredi;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = mManager.get(id);
			servicedata = mManager.getData(id);
			servicepredi = mManager.getDatapredi(id);
		} else if (StringUtils.isNotBlank(yearmonth) && uid != null && uid > 0) {
			entity = new Servicemain();
			entity.setDutycomp(uManager.get(uid));
			entity.setReportyearmonth(yearmonth);
			entity.setApplicateuser(SpringSecurityUtils.getCurrentUser());
		}
	}

	public void setId(Long id) {
		this.id = id;
	}

	// CRUD Action 函数 //

	public Page<Cominput> getPage() {
		return page;
	}

	@Override
	public String list() throws Exception {
		// districtlist = uManager.getDistrictlist(null,null, null);
		DateFormat df = new SimpleDateFormat("yyyyMM", java.util.Locale.CHINA);
		if (StringUtils.isBlank(yearmonth))
			yearmonth = df.format(DateUtils.add(DateUtils.getToday(),
					Calendar.MONTH, -1));
		if (StringUtils.isBlank(page.getOrderBy())) {
			page.setOrderBy("user.code");
			page.setOrder(Page.ASC);
		}
		User user = SpringSecurityUtils.getCurrentUser();
		Long udistrictid = user.getDistrict().getId();
		if (udistrictid != null && udistrictid > 0L) {
			districtid = udistrictid;
		}
		if (districtid == null)
			districtid = 0L;
		Integer uc = user.getCode();
		if (uc != null && uc.intValue() > 0) {
			usercode = uc;
		}
		if (districtid != null) {
			if (usercode == null) {
				inputcount = mManager.getInputCount(yearmonth, districtid);
			}
			page = mManager.getSmPageByDate(page, yearmonth, usercode,districtid);
		}
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		Long smid = mManager.getSmId(entity.getReportyearmonth(), entity
				.getDutycomp().getId());
		if (smid != null && smid > 0)
			entity.setId(smid);
		mManager.saveService(entity, servicedata, servicepredi);
		/*
		 * if(entity.getId()==null) {//first insert mManager.save(entity); }
		 */

		// 扩展指标
		HttpServletRequest request = Struts2Utils.getRequest();
		String value1 = "", value2 = "", value3 = "";
		for (Element e : entity.getDutycomp().getType().getElements()) {
			value1 = request.getParameter("vfirst" + e.getId());
			value2 = request.getParameter("vsecond" + e.getId());
			value3 = request.getParameter("vthird" + e.getId());
			Indicator i = entity.getIndicatorMap().get(e);// mManager.getIndicator(e,
															// entity);
			if (i == null) {
				i = new Indicator(new Empk(e, entity));
			}
			int changed = 0;
			if (StringUtils.isNotBlank(value1)) {
				i.setValue1(Double.valueOf(value1));
				changed = 1;
			}
			if (StringUtils.isNotBlank(value2)) {
				i.setValue2(Double.valueOf(value2));
				changed = 1;
			}
			if (StringUtils.isNotBlank(value3)) {
				i.setValue3(Double.valueOf(value3));
				changed = 1;
			}
			if (changed == 1)
				entity.getIndicatorMap().put(e, i);
		}
		mManager.save(entity);
		addActionMessage("保存报表成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			mManager.delete(id);
			addActionMessage("删除成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	public String getYearmonth() {
		return yearmonth;
	}

	public void setYearmonth(String yearmonth) {
		this.yearmonth = yearmonth;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public Integer getUsercode() {
		return usercode;
	}

	public void setUsercode(Integer usercode) {
		this.usercode = usercode;
	}

	public int getInputcount() {
		return inputcount;
	}

	public Long getDistrictid() {
		return districtid;
	}

	public void setDistrictid(Long districtid) {
		this.districtid = districtid;
	}

	public List<District> getDistrictlist() {
		return districtlist;
	}

	// 其他属性访问函数与Action函数 //

}
